package com.maolong.controller.login;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maolong.entity.domain.User;
import com.maolong.entity.format.Code;
import com.maolong.entity.format.RespResult;
import com.maolong.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class RegisterController {

    @Autowired
    LoginService loginService;
    /**
     * 接收异步请求，判断账号是否存在
     * @return
     */
    @GetMapping("/register")
    public RespResult registerCheck(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User one = loginService.getOne(queryWrapper);
        return new RespResult(one!=null? Code.REGISTERED: Code.UNREGISTERED,null,"");
    }

    /**
     * 接收注册信息，注册完成
     * @param user
     * @return
     */
    @PostMapping("/register")
    public RespResult register(@RequestBody User user){
        log.info("传入的注册信息是:{}",user);
        boolean save = loginService.save(user);
        return new RespResult(save? Code.REGISTER_SUCCESS: Code.ERROR,null,"注册成功");
    }
}
